import { isArray } from '@neu/biz-core/utils/utils'

class Printer {
  constructor() {
    // this.width = 719
    // this.height = 320
    this.wsocket = null
    this.printerConfig = null
    this.printDPI = 300 // 打印机的分辨率
    this.labelwidth = 67.5 // 标签宽度，单位是mm，注意换算
    this.labelheight = 28.5 // 标签高度，单位是mm，注意换算
    this.gapheight = 2 // 标签间隙，单位是mm，注意换算。如果这里设置为0，则打印机认为是连续纸。

    this.dotspermm = this.printDPI / 25.4 // 1mm换算为dot的数量
    this.printSpeed = 4 // 打印速度，值范围2~8，单位是英寸每秒
    this.printDarkness = 15 // 打印黑度，值范围0~20，值越大，打印温度越高
    this.labelheight_dots = parseInt(this.labelheight * this.dotspermm + 0.5) // 标签高度，单位是dot，四舍五入
    this.labelwidth_dots = parseInt(this.labelwidth * this.dotspermm + 0.5) // 标签宽度，单位是dot，四舍五入
    this.gapheight_dots = parseInt(this.gapheight * this.dotspermm + 0.5) // 标签间隙，单位是dot，四舍五入
    this.leftMargin_dots = 0
    this.belongs = 'kj'
  }

  // connect(ip, port, ok, error) {
  connect(printerConfig, ok, error) {
    this.printerConfig = printerConfig
    this.belongs = printerConfig.belongs
    if (this.printerConfig.leftMargin) {
      this.leftMargin_dots = this.printerConfig.leftMargin * this.dotspermm
    }
    const wsserver = 'ws://' + printerConfig.ip + ':' + printerConfig.port + '/'
    if ('WebSocket' in window) {
      this.wsocket = new window.WebSocket(wsserver)
    } else if ('MozWebSocket' in window) {
      this.wsocket = new window.MozWebSocket(wsserver)
    } else {
      console.log('浏览器不支持ws')
      error('浏览器不支持ws')
    }

    // 注册各类回调
    this.wsocket.onopen = function() {
      console.log('连接打印后台成功')
      ok('连接打印后台成功')
    }
    this.wsocket.onclose = function(event) {
      console.log('与打印后台断开连接')
      error('与打印后台断开连接，请检查打印机设置')
    }
    this.wsocket.onerror = function(error) {
      console.error(error)
      // error('数据传输错误')
    }
    this.wsocket.onmessage = function(receiveMsg) {
      console.log(receiveMsg.data)
    }
  }

  printLabel(data) {
    const that = this
    if (!isArray(data)) {
      data = [data]
    }
    let i = 0
    let timeoutId
    const next = data[i]
    print(next)

    function print(item) {
      that.wsocket.send('OpenPort|255') // 打开端口
      that.wsocket.send('ZM_ClearBuffer') // 清空
      that.wsocket.send('ZM_SetPrintSpeed|' + that.printSpeed) // 设置速度
      that.wsocket.send('ZM_SetDarkness|' + that.printDarkness) // 设置黑度
      that.wsocket.send('ZM_SetLabelHeight|' + that.labelheight_dots + '|' + that.gapheight_dots) // 设置标签高度和间隙
      that.wsocket.send('ZM_SetLabelWidth|' + that.labelwidth_dots) // 设置标签宽度

      // 第一行
      that.wsocket.send('ZM_DrawRectangle|' + parseInt(0.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(0.5 * that.dotspermm) + '|2' + '|' + parseInt(67.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(5.5 * that.dotspermm + 0.5))
      // 第二行
      that.wsocket.send('ZM_DrawRectangle|' + parseInt(0.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(5.5 * that.dotspermm + 0.5) + '|2' + '|' + parseInt(27.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(11 * that.dotspermm + 0.5))
      that.wsocket.send('ZM_DrawRectangle|' + parseInt(27.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(5.5 * that.dotspermm + 0.5) + '|2' + '|' + parseInt(67.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt((that.belongs === 'kj' ? 11 : 16.5) * that.dotspermm + 0.5))
      // 第三行
      that.wsocket.send('ZM_DrawRectangle|' + parseInt(0.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(11 * that.dotspermm + 0.5) + '|2' + '|' + parseInt(27.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(16.5 * that.dotspermm + 0.5))
      if (that.belongs === 'kj') {
        that.wsocket.send('ZM_DrawRectangle|' + parseInt(27.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(11 * that.dotspermm + 0.5) + '|2' + '|' + parseInt(67.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(16.5 * that.dotspermm + 0.5))
      }
      // 第四行
      that.wsocket.send('ZM_DrawRectangle|' + parseInt(0.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(16.5 * that.dotspermm + 0.5) + '|2' + '|' + parseInt(27.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(21.5 * that.dotspermm + 0.5))
      that.wsocket.send('ZM_DrawRectangle|' + parseInt(27.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(16.5 * that.dotspermm + 0.5) + '|2' + '|' + parseInt(67.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(26.5 * that.dotspermm + 0.5))
      // 第五行
      that.wsocket.send('ZM_DrawRectangle|' + parseInt(0.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(21.5 * that.dotspermm + 0.5) + '|2' + '|' + parseInt(27.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(26.5 * that.dotspermm + 0.5))
      if (item.belongs === 'kj') {
        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(11.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(1 * that.dotspermm + 0.5) + '|44|0|宋体|1|600|0|0|0|A1|' + that.printerConfig.labelTitle)
        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(1.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(7 * that.dotspermm + 0.5) + '|27|0|宋体|1|600|0|0|0|A2|分类：')
        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(7.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(7 * that.dotspermm + 0.5) + '|27|0|宋体|1|600|0|0|0|A3|' + item.smallTypeName)
        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(28.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(7 * that.dotspermm + 0.5) + '|27|0|宋体|1|600|0|0|0|A4|编码：')
        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(34.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(7 * that.dotspermm + 0.5) + '|27|0|宋体|1|600|0|0|0|A5|' + item.code)

        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(1.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(12.5 * that.dotspermm + 0.5) + '|27|0|宋体|1|600|0|0|0|A6|品牌：')

        const brand = item.brand ? item.brand : ''
        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(7.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(12.5 * that.dotspermm + 0.5) + '|27|0|宋体|1|600|0|0|0|A7|' + brand)
        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(28.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(12.5 * that.dotspermm + 0.5) + '|27|0|宋体|1|600|0|0|0|A8|型号：')
        const specifications = item.specifications ? item.specifications : ''
        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(34.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(12.5 * that.dotspermm + 0.5) + '|27|0|宋体|1|600|0|0|0|A9|' + specifications)

        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(1.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(18 * that.dotspermm + 0.5) + '|27|0|宋体|1|600|0|0|0|B1|购置时间：')
        const purchaseDate = item.purchaseDate ? item.purchaseDate.substr(0, 10) : ''
        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(12.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(18 * that.dotspermm + 0.5) + '|27|0|宋体|1|600|0|0|0|B2|' + purchaseDate)
        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(1.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(23 * that.dotspermm + 0.5) + '|27|0|宋体|1|600|0|0|0|B3|使用年限：')
        const usePeriod = item.usePeriod ? item.usePeriod : ''
        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(12.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(23 * that.dotspermm + 0.5) + '|30|0|宋体|1|600|0|0|0|B4|' + usePeriod)
        that.wsocket.send('ZM_DrawBarcode|' + parseInt(28.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(18 * that.dotspermm + 0.5) + '|0|1|3|3|90|N|' + item.code)
        let wirteCode = item.code
        if (wirteCode.length === 15) {
          wirteCode += 'X'
        }
        that.wsocket.send('ZM_RW_RfidFormat|1|1|0|' + wirteCode.length + '|1|' + wirteCode) // 写入RFID
        that.wsocket.send('ZM_PrintLabel_R|1|1') // 打印标签
        that.wsocket.send('ClosePort') // 关闭端口
      } else {
        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(11.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(1 * that.dotspermm + 0.5) + '|44|0|宋体|1|600|0|0|0|A1|' + that.printerConfig.labelTitle)
        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(1.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(7 * that.dotspermm + 0.5) + '|38|0|宋体|1|600|0|0|0|A2|分类：')
        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(9.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(7 * that.dotspermm + 0.5) + '|38|0|宋体|1|600|0|0|0|A3|' + item.smallTypeName.substring(0, 6))
        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(39.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(7 * that.dotspermm + 0.5) + '|38|0|宋体|1|600|0|0|0|A4|资产编码')
        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(32.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(11.5 * that.dotspermm + 0.5) + '|42|0|宋体|1|600|0|0|0|A5|' + item.code)

        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(1.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(12.5 * that.dotspermm + 0.5) + '|38|0|宋体|1|600|0|0|0|A6|名称：')
        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(9.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(12.5 * that.dotspermm + 0.5) + '|38|0|宋体|1|600|0|0|0|A7|' + item.name.substring(0, 6))
        // that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(28.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(12.5 * that.dotspermm + 0.5) + '|27|0|宋体|1|600|0|0|0|A8|购置时间：')
        // const purchaseDate = item.purchaseDate ? item.purchaseDate.substr(0, 10) : ''
        // that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(39.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(12.5 * that.dotspermm + 0.5) + '|27|0|宋体|1|600|0|0|0|A9|' + purchaseDate)
        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(1.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(18 * that.dotspermm + 0.5) + '|38|0|宋体|1|600|0|0|0|B1|品牌：')
        const brand = item.brand ? item.brand : ''
        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(9.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(18 * that.dotspermm + 0.5) + '|38|0|宋体|1|600|0|0|0|B2|' + brand.substring(0, 6))
        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(1.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(23 * that.dotspermm + 0.5) + '|38|0|宋体|1|600|0|0|0|B3|型号：')
        const specifications = item.specifications ? item.specifications : ''
        that.wsocket.send('ZM_DrawTextTrueTypeW|' + parseInt(9.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(23 * that.dotspermm + 0.5) + '|38|0|宋体|1|600|0|0|0|B4|' + specifications.substring(0, 6))
        that.wsocket.send('ZM_DrawBarcode|' + parseInt(28.75 * that.dotspermm + 0.5 + that.leftMargin_dots) + '|' + parseInt(18 * that.dotspermm + 0.5) + '|0|1|3|3|90|N|' + item.code)
        let wirteCode = item.code
        if (wirteCode.length === 15) {
          wirteCode += 'X'
        }
        that.wsocket.send('ZM_RW_RfidFormat|1|1|0|' + wirteCode.length + '|1|' + wirteCode) // 写入RFID
        that.wsocket.send('ZM_PrintLabel_R|1|1') // 打印标签
        that.wsocket.send('ClosePort') // 关闭端口
      }
      i++
      if (i < data.length) {
        const next = data[i]
        timeoutId = setTimeout(function() {
          print(next)
        }, 1500)
      } else {
        clearTimeout(timeoutId)
      }
    }
  }
}

export default new Printer()
